package com.minio.learning.springminio.controller;

import com.minio.learning.springminio.entity.FileInfo;
import com.minio.learning.springminio.entity.FileVO;
import com.minio.learning.springminio.entity.RemoveFailVO;
import com.minio.learning.springminio.resp.CommonResult;
import com.minio.learning.springminio.service.MinioService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * @author YanXxx
 * @date 2022-12-12 17:08
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/minio")
public class MinioController {
    @Resource
    private MinioService minioService;

    @Value("${minio.bucketName}")
    private String bucket;

    @GetMapping("/createBucket")
    public CommonResult<?> createBucket(@RequestParam(name = "bucketName")
                                            @NotBlank(message = "存储桶名称不能为空！") String bucketName) {
        boolean result = minioService.createBucket(bucketName);
        if (result) {
            return CommonResult.ok("创建桶成功！");
        } else {
            return CommonResult.error("创建桶失败！");
        }
    }

    @GetMapping("/deleteBucket")
    public CommonResult<?> deleteBucket(@RequestParam(name = "bucketName")
                                            @NotBlank(message = "存储桶名称不能为空！") String bucketName) {
        boolean result = minioService.deleteBucket(bucketName);
        if (result) {
            return CommonResult.ok("删除桶成功！");
        } else {
            return CommonResult.error("删除桶失败！");
        }
    }

    @PostMapping("/uploadFile")
    public CommonResult<List<FileVO>> uploadFile(@RequestParam(name = "file")
                                                     @NotEmpty(message = "上传文件不能为空！") MultipartFile[] files,
                                                 @RequestParam(name = "bucketName",required = false)
                                                 String bucketName) {
        if(StringUtils.isBlank(bucketName)){
            bucketName = bucket;
        }
        List<FileVO> fileVOS = minioService.uploadFile(bucketName, files);
        if (fileVOS.size() > 0) {
            return CommonResult.ok("上传成功！",fileVOS);
        } else {
            return CommonResult.error("上传失败！");
        }
    }

    @GetMapping("/download")
    public void uploadFile(@RequestParam(name = "filePath") @NotBlank(message = "下载路径不能为空！") String filePath,
                           HttpServletResponse response,
                           @RequestParam(name = "bucketName",required = false) String bucketName) {
        if(StringUtils.isBlank(bucketName)){
            bucketName = bucket;
        }
        minioService.download(bucketName, filePath, response);
    }

    @GetMapping("/deleteObjects")
    public CommonResult<List<RemoveFailVO>> deleteObjects(@RequestParam(name = "bucketName",required = false)
                                                              String bucketName,
                                                          @RequestParam("filePaths") List<String> filePaths) {
        if(StringUtils.isBlank(bucketName)){
            bucketName = bucket;
        }
        List<RemoveFailVO> removeFailVOS = minioService.deleteObjects(bucketName, filePaths);
        if (removeFailVOS.size() > 0) {
            return CommonResult.error("部分删除失败！", removeFailVOS);
        } else {
            return CommonResult.ok("删除成功");
        }
    }

    @GetMapping("/listBuckets")
    public CommonResult<List<String>> listBuckets() {
        List<String> buckets = minioService.listBuckets();
        return CommonResult.ok("查询成功！", buckets);
    }

    @GetMapping("/listFiles")
    public CommonResult<List<FileInfo>> listBuckets(@RequestParam(name = "bucketName",required = false) String bucketName,
                                                    @RequestParam(name = "prefix", required = false) String prefix) {
        if(StringUtils.isBlank(bucketName)){
            bucketName = bucket;
        }
        List<FileInfo> fileInfos = minioService.listFiles(bucketName, prefix);
        return CommonResult.ok("查询成功！", fileInfos);
    }
}
